package com.flyco.modules.stm.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.modules.stm.entity.StmChannelProductApply;
import com.flyco.modules.stm.entity.StmChannelProductApplyItem;
import com.flyco.modules.stm.service.IStmChannelProductApplyService;
import com.flyco.modules.stm.service.IStmChannelProductService;
import com.flyco.modules.stm.service.IStmChannelProductApplyItemService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 申请明细表
 * @Author: flyco
 * @Date: 2023-06-21
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "申请明细表")
@RestController
@RequestMapping("/f/stm/stmChannelProductApplyItem")
public class StmChannelProductApplyItemController extends BaseController<StmChannelProductApplyItem, IStmChannelProductApplyItemService> {
    @Autowired
    private IStmChannelProductApplyItemService stmChannelProductApplyItemService;
    @Autowired
    private IStmChannelProductService productService;
    @Autowired
    private IStmChannelProductApplyService applyService;


    /**
     * 分页列表查询
     *
     * @param stmChannelProductApplyItem
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "申请明细表-分页列表查询")
    @ApiOperation(value = "申请明细表-分页列表查询", notes = "申请明细表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(StmChannelProductApplyItem stmChannelProductApplyItem,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        IPage<StmChannelProductApplyItem> pageList = new Page<>();
        if (stmChannelProductApplyItem == null || stmChannelProductApplyItem.getApplyId() == null) {
            return Result.ok(pageList);
        }
        QueryWrapper<StmChannelProductApplyItem> queryWrapper = QueryGenerator.initQueryWrapper(stmChannelProductApplyItem, req.getParameterMap());
        Page<StmChannelProductApplyItem> page = new Page<StmChannelProductApplyItem>(pageNo, pageSize);
        pageList = stmChannelProductApplyItemService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param stmChannelProductApplyItem
     * @return
     */
    @AutoLog(value = "申请明细表-添加")
    @ApiOperation(value = "申请明细表-添加", notes = "申请明细表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody StmChannelProductApplyItem stmChannelProductApplyItem) {
        stmChannelProductApplyItemService.save(stmChannelProductApplyItem);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param stmChannelProductApplyItem
     * @return
     */
    @AutoLog(value = "申请明细表-编辑")
    @ApiOperation(value = "申请明细表-编辑", notes = "申请明细表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StmChannelProductApplyItem stmChannelProductApplyItem) {
        stmChannelProductApplyItemService.updateById(stmChannelProductApplyItem);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "申请明细表-通过id删除")
    @ApiOperation(value = "申请明细表-通过id删除", notes = "申请明细表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        stmChannelProductApplyItemService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "申请明细表-批量删除")
    @ApiOperation(value = "申请明细表-批量删除", notes = "申请明细表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.stmChannelProductApplyItemService.removeByIds(Arrays.asList(ids.split(",")));

        StmChannelProductApplyItem info = stmChannelProductApplyItemService.getById(Arrays.asList(ids.split(",")).get(0));
        if(info!=null){
            applyService.updateApplyStatusByApprove(info.getChannelId());
        }
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "申请明细表-通过id查询")
    @ApiOperation(value = "申请明细表-通过id查询", notes = "申请明细表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        StmChannelProductApplyItem stmChannelProductApplyItem = stmChannelProductApplyItemService.getById(id);
        return Result.ok(stmChannelProductApplyItem);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param stmChannelProductApplyItem
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StmChannelProductApplyItem stmChannelProductApplyItem) {
        return super.exportXls(request, stmChannelProductApplyItem, StmChannelProductApplyItem.class, "申请明细表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StmChannelProductApplyItem.class);
    }

    /**
     * 渠道申请价格审批通过
     */
    @AutoLog(value = "申请明细表-审核通过")
    @ApiOperation(value = "申请明细表-审核通过", notes = "申请明细表-审核通过")
    @GetMapping(value = "/approve")
    public Result<?> approve(String ids,Integer status) {

//        LambdaQueryWrapper<StmChannelProductApplyItem> applyItemWrapper=new LambdaQueryWrapper<>();
//        applyItemWrapper.in(StmChannelProductApplyItem::getId,ids.split(","));
//        StmChannelProductApplyItem applyItem=new StmChannelProductApplyItem();
//        applyItem.setStatus(status);
//        stmChannelProductApplyItemService.update(applyItem,applyItemWrapper);
//
//        if(status==3){
//            //把审批通过的记录更新到表stm_channel_product中
//            productService.mergeIntoChannelProduct(ids);
//        }
        return Result.ok("审批成功！");
    }


    /**
     * 编辑
     */
    @AutoLog(value = "申请明细表-编辑")
    @ApiOperation(value = "申请明细表-编辑", notes = "申请明细表-编辑")
    @PutMapping(value = "/editItemPrice")
    public Result<?> editItemPrice(@RequestBody List<StmChannelProductApplyItem> item) {
        if (!CollectionUtils.isEmpty(item)){
            stmChannelProductApplyItemService.updateBatchById(item);
            StmChannelProductApply apply=applyService.getById(item.get(0).getApplyId());
            if(!apply.getStatus().equals(1) && !apply.getStatus().equals(2)){
                return Result.error("该申请状态不允许修改价格");
            }
            if(apply.getStatus().equals(2)){
                apply.setStatus(1);
                applyService.updateById(apply);

                //发送价格审核邮件通知
                stmChannelProductApplyItemService.sendMailForUpdatePrice(apply.getApplyId());
            }
        }
        return Result.ok("编辑成功!");
    }
}
